<template>
  <div id="task">
    <div class="task">
      <div class="min_bg"><img :src="img" alt=""/><b>拥有 <span>{{nest}}</span> 枚U币</b></div>
      <div class="today_task">
          <p class="perTitle"><i></i><span>今日任务</span></p>
          <ul>
              <li v-for="(ta, index) in task" :key="index" >
                  <div class="icon"><img :src="ta.icon_path" alt=""/><i></i><span>{{ta.name}}</span></div>
                  <div class="sulg"><span>+{{ta.reward}}</span>U币</div>
                  <div class="fished">
                      <input v-if="ta.is_finished == false" @click="finish(ta.slug,ta.component)" type="button" value="去完成"/>
                      <input v-if="ta.is_finished == true" style="background-color: #c7c7c7;cursor: not-allowed" type="button" value="已完成" disabled/>
                  </div>
              </li>
          </ul>
      </div>
    </div>
    <!-- 签到弹窗 -->
    <div class="popup" id="signSu" v-if="sucShow">
      <div class="popup_vanish"></div>
      <div class="popupN">
          <div class="sign_s">
              <div class="signT">签 到 成 功</div>
              <img :src="sucs" alt=""/>
              <p class="signS">签到成功 U币 <span class="organColor">+{{reward}}</span></p>
              <div class="Btn"><el-button type="success" size='small' style="margin-top: 30px;" @click="sucShow = false">确定</el-button></div>
          </div>
      </div>
    </div>
    <!-- 邀请弹窗 -->
    <div class="popup" id="inviteT" v-if="invitShow">
      <div class="popup_vanish"></div>
      <div class="popupN">
          <textarea class="copyContent" id="clone" style="width: 94%; resize:none;">{{copyContent}}</textarea>
          <p class="copyT">复制上面内容发送给好友，好友成功注册后即可获得U币</p>
          <div class="Btn"><el-button type="success" size='small' data-clipboard-action="copy" data-clipboard-target="#copyContent" @click="copy">复制</el-button></div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'task',
  data () {
    return {
      sucs: require('../../assets/images/success.png'),
      sucShow: false,
      invitShow: false,
      img: require('../../assets/images/nest.png'),
      task: [],
      nest: '',
      reward: [],
      copyContent: ''
    };
  },
  created () {
    this.getdata();
    this.getwallet();
  },
  mounted () {
    // 监听反馈信息
    this.$bus.$on('feedSucc', () => {
      this.getdata();
      this.getwallet();
    });
  },
  methods: {
    copy () {
      this.invitShow = false;
      let Url = document.getElementById('clone');
      Url.select(); // 选择对象
      document.execCommand('Copy'); // 执行浏览器复制命令
      if (document.execCommand('Copy')) {
        this.$message({
          message: '复制成功',
          type: 'success'
        });
      } else {
        this.$message({
          message: '若复制失败，请手动复制',
          type: 'warning'
        });
      }
    },
    // 按钮点击事件
    finish (slug, component) {
      this.axios
        .get(`task/check/${slug}`)
        .then(({ data }) => {
          if (data.code === 200) {
            if (data.data.is_finished === false) {
              switch (component) {
                case 'invite':
                  this.invite(slug);
                  break;
                case 'sign':
                  this.sign(slug);
                  break;
                case 'feed-back':
                  this.feedBack(slug);
                  break;
                case 'share':
                  // location.href = '/niaowo/invitedShare.html';
                  this.$router.push({name: 'invitedShare'});
                  break;
                case 'questionnaire':
                  // top.location.href = '/crm/survey';
                  this.$router.push({name: 'survey'});
                  break;
              }
            } else {
              this.$message({
                message: '此任务已完成，请勿重复',
                type: 'warning'
              });
            }
          } else {
            this.$message({
              message: data.msg,
              type: 'warning'
            });
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    // 反馈意见
    feedBack () {
      this.$bus.$emit('feed');
    },
    // 签到
    sign () {
      this.axios.post('task/exec/sign')
        .then(({ data }) => {
          if (data.code === 200) {
            this.sucShow = true;
            this.getdata();
            this.getwallet();
          } else {
            this.$message({
              message: data.msg,
              type: 'warning'
            });
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    // 邀请
    invite (sulg) {
      this.axios.get('task/inviteContent/invite')
        .then(({data}) => {
          if (data.code === 200) {
            this.invitShow = true;
            this.copyContent = data.data.content;
          } else {
            this.$message({
              message: data.msg,
              type: 'warning'
            });
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    // 获取金币user/wallet
    getwallet () {
      this.axios
        .get('user/wallet')
        .then(({ data }) => {
          if (data.code === 200) {
            this.nest = data.data.point;
          } else {
            this.$message({
              message: data.msg,
              type: 'warning'
            });
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    // 获取列表信息
    getdata () {
      this.axios
        .get('task', {
          params: {
            type: 1
          }
        })
        .then(({ data }) => {
          if (data.code === 200) {
            this.task = data.data.lists;
            this.reward = data.data.lists[0].reward;
          } else {
            this.$message({
              message: data.msg,
              type: 'warning'
            });
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    }
  }
};
</script>

<style lang="scss" scoped>
#task {
  .task .min_bg {
    text-align: center;
    background: url("../../assets/images/mine_bg.png") no-repeat;
    background-size: 100% 100%;
    padding: 20px;
    overflow: hidden;
  }
  .task .min_bg img {
    width: 240px;
    vertical-align: middle;
    margin-left: -80px;
  }
  .task .min_bg b {
    font-size: 22px;
    color: #424242;
    font-weight: inherit;
    letter-spacing: 2px;
    position: relative;
    top: 10px;
  }
  .task .min_bg b span {
    color: #10a57d;
    font-size: 40px;
    margin: 0 15px;
  }
  .task .today_task {
    margin-top: 20px;
    font-size: 16px;
  }
  .task .today_task {
    margin-top: 20px;
    font-size: 16px;
  }
  .perTitle {
    /*padding: 10px 0;*/
    height: 40px;
    border-bottom: 1px solid #dfdfdf;
    font-size: 16px;
  }
  .perTitle i {
    width: 5px;
    display: inline-block;
    height: 16px;
    vertical-align: middle;
    background-color: #10a57d;
    line-height: 40px;
    margin: 0 10px;
  }
  .perTitle .right {
    float: right;
    cursor: pointer;
  }
  .perTitle .icon-shanchu,
  .icon-wancheng {
    color: #f75a40;
    font-size: 20px;
  }
  .perTitle .icon-wancheng {
    color: green;
  }
  .perTitle .text {
    line-height: 40px;
  }
  .task .today_task ul li {
    padding: 20px 5px;
    border-bottom: 1px solid #eef3f7;
  }
  .task .today_task ul li div {
    display: inline-block;
    width: 32.5%;
    color: #424242;
  }
  .task .today_task ul li .icon img {
    width: 30px;
    vertical-align: middle;
  }
  .task .today_task ul li .icon i {
    width: 2px;
    height: 16px;
    vertical-align: middle;
    display: inline-block;
    margin: 0 10px;
    background-color: #dfdfdf;
  }
  .task .today_task ul li .sulg span {
    color: #ed691a;
    margin-right: 10px;
  }
  .task .today_task ul li .fished {
    text-align: right;
  }
  .task .today_task ul li .fished input {
    text-align: center;
    width: 160px;
    color: #fff;
    background-color: #ff3d3d;
    padding: 8px 0;
    border-radius: 30px;
    cursor: pointer;
  }
  /*弹窗*/
.popup{
    position: fixed;
    top:0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.6);
    z-index: 666;
}
.popup .popup_vanish{
    position: relative;
    top:0;
    left: 0;
    width: 100%;
    height: 100%;
}
.popup .popupN{
    width: 500px;
    background-color: #fff;
    padding: 30px;
    border-radius: 10px;
    position: absolute;
    top: 30%;
    left: 50%;
    margin-left: -250px;
}
.popup .popupN .grayP {
  color: #999999;
}
.popup .popupN .closeQuestion {
    position: absolute;
    font-size: 30px;
    color: #C2C2C2;
    top: 10px;
    cursor: pointer;
    right: 20px;
}
/*签到成功*/
.sign_s{
    text-align: center;
}
.sign_s .signT{
    background: url("../../assets/images/signB.png") no-repeat;
    background-size: 100% 100%;
    color: #fff;
    height: 45px;
    line-height: 38px;
    font-size: 18px;
}
.sign_s img{
    width: 90px;
    margin: 20px 0;
}
.signS{
    font-size: 16px;
}
.feed p{
    font-size: 16px;
    color: #424242;
    margin: 10px 0;
}
.feed textarea{
    width: 100%;
    padding: 10px;
    height: 100px;
    border: 1px solid #dcdcdc;
    resize: none;
    outline: none;
    border-radius: 5px;
}
// 复制弹窗
/*复制链接*/
.copyContent{
    border: 1px solid #dcdcdc;
    border-radius: 5px;
    height: 120px;
    padding: 15px;
    word-break: break-all;
}
.copyT{
    color: #c4c4c4;
    font-size: 12px;
    margin-top: 10px;
}
.Btn{
    text-align: center;
    margin-top: 20px;
}
.organColor{
    color: #f16027;
}
}
</style>
